package me.acomma.groot.domain.role;

import me.acomma.groot.domain.user.UserId;
import me.acomma.groot.infrastructure.dao.RoleDao;
import me.acomma.groot.infrastructure.id.ID;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleRepository {
    @Autowired
    private RoleDao roleDao;

    public long countByUserId(UserId userId) {
        if (userId == null) {
            return 0;
        }
        List<Role> roles = roleDao.selectByUserId(userId);
        return CollectionUtils.isEmpty(roles) ? 0 : roles.size();
    }

    public Role get(RoleId roleId) {
        return roleDao.select(roleId);
    }

    public RoleId nextId() {
        return new RoleId(ID.next());
    }

    public void add(Role role) {
        roleDao.insert(role);
    }

    public void update(Role role) {
        roleDao.update(role);
    }

    public void remove(RoleId roleId) {
        roleDao.delete(roleId);
    }

    public List<Role> getAll() {
        return roleDao.selectAll();
    }

    public List<Role> pageQuery(Role role) {
        return roleDao.pagedQuery(role);
    }

    public List<Role> getByUserId(UserId userId) {
        return roleDao.selectByUserId(userId);
    }

    public void removeRoleMenuByRoleId(RoleId roleId) {
        roleDao.deleteRoleMenuByRoleId(roleId);
    }

    public void addRoleMenu(Long roleId, List<Long> menuIds) {
        roleDao.insertRoleMenu(roleId, menuIds);
    }
}
